package cn.edu.zafu.bigdata.x.Foo;

import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.util.Date;

public class TimeClient {
    public static void main(String[] args) throws IOException {
        int port = 3000; // 服务端端口
        InetAddress serverAddress = InetAddress.getByName("192.168.16.38"); // 替换为同学电脑的IP地址
        DatagramSocket clientSocket = new DatagramSocket();
        byte[] sendData = new byte[1024];
        byte[] receiveData = new byte[1024];

        // 发送请求到服务端
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, port);
        clientSocket.send(sendPacket);

        // 接收服务端响应
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        clientSocket.receive(receivePacket);

        // 获取服务端发送的时间戳
        long serverTime = ByteBuffer.wrap(receivePacket.getData()).getLong();

        // 打印服务端的时间
        System.out.println("Server time is: " + new Date(serverTime));
    }
}
